package com.baiyue.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.baiyue.entity.Log;
import com.baiyue.entity.UsernameEntity;
import com.baiyue.mapper.LogMapper;
import com.baiyue.model.log.LogDataRequest;
import com.baiyue.service.LogService;
import com.baiyue.service.UserService;
import com.baiyue.utils.BeanMapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author WuBo
 */
@Service
public class LogServiceImpl implements LogService {
    private static final Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest httpRequest;

    @Override
    public Integer insert(Log log) {
        return logMapper.insert(log);
    }

    /**
     * 分页查询用户列表
     * @param request
     * @return 返回json
     */
    @Override
    public Page<Log> findAll(LogDataRequest request) {
        logger.info("查询log日志信息入参:{}", JSON.toJSONString(request));
        userService.getLoginUser(httpRequest);
        Page<Log> pageable = new Page<>(request.getPage(),request.getSize());
        Log logRequest = BeanMapperUtil.map(request,Log.class);
        return pageable.setRecords(logMapper.findAll(logRequest,pageable));
    }

    @Override
    public List<UsernameEntity> findByUsername() {
        return logMapper.findLogUser();
    }

}
